package com.luozongyao.videoPromotion.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luozongyao.commons.R;
import com.luozongyao.utils.DateUtil;
import com.luozongyao.videoPromotion.entity.Video;
import com.luozongyao.videoPromotion.entity.vo.VideoVo;
import com.luozongyao.videoPromotion.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luozongyao
 * @since 2021-11-07
 */
@Api(description="视频推广信息管理")
@RestController
@RequestMapping("/videoPromotion/video")
@CrossOrigin
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private RedisTemplate stringRedisTemplate;

    //获取某天视频发布数
    @ApiOperation(value = "获取某天视频发布数")
    @GetMapping("VideoCount/{day}")
    public R getVideoCount(@PathVariable String day){
        Integer VideoCount= videoService.getVideoCount(day);
        return R.ok().data("VideoCount",VideoCount);
    }


    @ApiOperation(value = "通过videoId查询指定视频")
    @GetMapping("getVideoInfo/{id}")
    public R getVideoInfo(@PathVariable int id){
        if (StringUtils.containsWhitespace(String.valueOf(id))){
            String newId = String.valueOf(id).replace(" ", "");
            id = Integer.parseInt(newId);
        }
        QueryWrapper<Video> wrapper = new QueryWrapper<Video>();
        wrapper.eq("video_id",id);
        wrapper.eq("is_pass",1);
        List<Video> videoList = videoService.list(wrapper);
        System.out.println(videoList);
        return R.ok().data("video",videoList);
    }
    @ApiOperation(value = "设置推广视频Id到redis")
    @GetMapping("setVideoPromotion/{id}")
    public R setVideoPromotion(@PathVariable int id){
        stringRedisTemplate.opsForSet().add("videoPromotion",id+"");
//        System.out.println("resultSet:"+resultSet);
        return R.ok();
    }
    @ApiOperation(value = "判断redis推广视频Id是否已存在")
    @GetMapping("judgeVideoPromotion/{id}")
    public R judgeVideoPromotion(@PathVariable int id){
        boolean videoPromotion = stringRedisTemplate.opsForSet().members("videoPromotion").contains(id + "");
        return R.ok().data("result",videoPromotion);
    }


    @ApiOperation(value = "取消指定推广视频Id到redis")
    @GetMapping("cancelVideoPromotion/{id}")
    public R cancelVideoPromotion(@PathVariable int id){

        stringRedisTemplate.opsForSet().remove("videoPromotion", id + "");
        return R.ok();
    }


    @ApiOperation(value = "查询已推广列表")
    @GetMapping("getAlreadyPromotion")
    public R getAlreadyPromotion(){
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        //获取videoPromotion set集合
        Set videoPromotion = stringRedisTemplate.opsForSet().members("videoPromotion");
        List<Video> videoList = new ArrayList<>();
        for (Object obj : videoPromotion) {
            Video video = videoService.getById((Serializable) obj);
            videoList.add(video);
        }
        return R.ok().data("videoList",videoList);
    }
    //获取视频播放量前10的视频标题和视频播放量
    @ApiOperation(value = "获取视频播放量前10的视频标题和视频播放量")
    @GetMapping("/getTopTenVideo")
    public R getTopTenVideo(){
        List<VideoVo> videoViewData = new ArrayList<>();
        stringRedisTemplate.opsForZSet().reverseRangeWithScores("videoViewsRank",0,9).forEach(
                item->{
                    DefaultTypedTuple tuple = (DefaultTypedTuple) item;
                    String view = (int) Math.ceil(tuple.getScore())+"";
                    Object videoID = tuple.getValue();
                    Video video = videoService.getById((Serializable) videoID);
                    String videoTitle = video.getVideoTitle();
                    VideoVo videoVo = new VideoVo();
                    videoVo.setVideoTitle(videoTitle);
                    videoVo.setView(view);
                    videoViewData.add(videoVo);
                }
        );
        return R.ok().data("videoViewData",videoViewData);
    }
    //获取视频播放量前10的视频标题和视频点赞量
    @ApiOperation(value="获取视频点赞量前10的视频标题和视频点赞量")
    @GetMapping("/getTopTenVideoLike")
    public R getTopTenVideoLike(){
        List<VideoVo> videoLikeData = new ArrayList<>();

        stringRedisTemplate.opsForZSet().reverseRangeWithScores("videoLikesRank",0,9).forEach(
                item->{
                    DefaultTypedTuple tuple = (DefaultTypedTuple) item;
                    String like = (int) Math.ceil(tuple.getScore())+"";
                    Object videoID = tuple.getValue();
                    Video video = videoService.getById((Serializable) videoID);
                    String videoTitle = video.getVideoTitle();
                    VideoVo videoVo = new VideoVo();
                    videoVo.setVideoTitle(videoTitle);
                    videoVo.setLike(like);
                    videoLikeData.add(videoVo);
                }
        );
        return R.ok().data("videoLikeData",videoLikeData);
    }

    @ApiOperation(value="获取指定类型视频播放量前10的视频标题和视频点赞量")
    @GetMapping("/getTopTenVideoLike/{type}")
    public R getTopTenVideoLike(@PathVariable String type){
        List<VideoVo> videoLikeData = new ArrayList<>();
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("is_pass",1);
        queryWrapper.orderByDesc("likes");
        switch (type) {
            case "sevenDays":
                queryWrapper.between("check_time",DateUtil.addDays(new Date(),-7),new Date());
                List<Video> videoList = videoService.list(queryWrapper);
                for (int i = 0; i < videoList.size(); i++) {
                    if (i>=10){
                        break;
                    }
                    Video video = videoList.get(i);
                    VideoVo videoVo = new VideoVo();
                    videoVo.setVideoTitle(video.getVideoTitle());
                    videoVo.setLike(video.getLikes()+"");
                    videoLikeData.add(videoVo);
                }
                break;
            case "aMonth":
                queryWrapper.between("check_time",DateUtil.addDays(new Date(),-30),new Date());
                List<Video> videoList2 = videoService.list(queryWrapper);
                for (int i = 0; i < videoList2.size(); i++) {
                    if (i>=10){
                        break;
                    }
                    Video video = videoList2.get(i);
                    VideoVo videoVo = new VideoVo();
                    videoVo.setVideoTitle(video.getVideoTitle());
                    videoVo.setLike(video.getLikes()+"");
                    videoLikeData.add(videoVo);
                }
                break;
            default:
                break;
        }
        return R.ok().data("videoLikeData",videoLikeData);
    }
    @ApiOperation(value="获取指定类型视频播放量前10的视频标题和视频点赞量")
    @GetMapping("/getTopTenVideoView/{type}")
    public R getTopTenVideoView(@PathVariable String type){
        List<VideoVo> videoViewData = new ArrayList<>();
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("is_pass",1);
        queryWrapper.orderByDesc("likes");
        switch (type) {
            case "sevenDays":
                queryWrapper.between("check_time",DateUtil.addDays(new Date(),-7),new Date());
                List<Video> videoList = videoService.list(queryWrapper);
                for (int i = 0; i < videoList.size(); i++) {
                    if (i>=10){
                        break;
                    }
                    Video video = videoList.get(i);
                    VideoVo videoVo = new VideoVo();
                    videoVo.setVideoTitle(video.getVideoTitle());
                    videoVo.setView(video.getViews()+"");
                    videoViewData.add(videoVo);
                }
                break;
            case "aMonth":
                queryWrapper.between("check_time",DateUtil.addDays(new Date(),-30),new Date());
                List<Video> videoList2 = videoService.list(queryWrapper);
                for (int i = 0; i < videoList2.size(); i++) {
                    if (i>=10){
                        break;
                    }
                    Video video = videoList2.get(i);
                    VideoVo videoVo = new VideoVo();
                    videoVo.setVideoTitle(video.getVideoTitle());
                    videoVo.setView(video.getViews()+"");
                    videoViewData.add(videoVo);
                }
                break;
            default:
                break;
        }
        return R.ok().data("videoViewData",videoViewData);
    }


}

